package com.wuxuan.javabase_homework.homework3;

import com.sun.tools.javac.Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

/**
 * 商户界面
 */
public class MerchantScreen {
    private static Scanner sc = new Scanner(System.in);

    private People merchant;
    //当前商户的所有店铺
    private List<Shop> shops;

    MerchantScreen(People merchant) {
        this.merchant = merchant;
        setThisShops();
    }


    /**
     * 商户界面
     */
    public void menu() {
        while (true) {
            System.out.println("======================当前商户信息========================");
            System.out.println("用户ID:" + merchant.getId());
            System.out.println("用户名:" + merchant.getName());
            System.out.println("余额:" + merchant.getBalance());
            System.out.println("======================商户界面========================");
            System.out.println("0.退出登录");
            System.out.println("1.充值");
            System.out.println("2.创建店铺");
            System.out.println("3.删除店铺");
            System.out.println("4.编辑店铺");
            System.out.println("5.查看自己所有店铺");
            System.out.println("6.进入自己店铺");
            int choose = sc.nextInt();
            switch (choose) {
                case 0:
                    MainScreen.menu();
                    return;
                case 1:
                    recharge();
                    break;
                case 2:
                    addShop();
                    break;
                case 3:
                    deleteShop();
                    break;
                case 4:
                    updateShop();
                    break;
                case 5:
                    selectAllShops();
                    break;
                case 6:
                    goToShop();
                    break;
                default:
                    System.out.println("请输入正确的序号！");
            }
        }
    }

    /**
     * 充值
     */
    private void recharge() {
        System.out.println("======================充值界面========================");
        while (true) {
            System.out.println("请输入您要充值的金额：");
            double recharge = sc.nextDouble();
            if (recharge >= 0 && recharge <= 1000000) {
                merchant.setBalance(merchant.getBalance() + recharge);
                break;
            } else {
                System.out.println("金额异常！请重新输入！");
            }
        }
    }

    /**
     * 设置当前商户的店铺信息(保持与MainScreen中的类似数据库中的同步)
     */
    private void setThisShops() {
        //从总店铺中获取当前商户的店铺，初始化在这个界面中
        shops = new ArrayList<>();
        for (Shop shop : MainScreen.shops) {
            if (shop.getMerchant() == merchant) {
                shops.add(shop);
            }
        }
    }

    /**
     * 添加店铺
     */
    private void addShop() {
        System.out.println("======================创建店铺========================");
        while (true) {
            System.out.println("请输入店铺名：");
            String name = sc.next();

            //一开始没有店铺的话，肯定是不存在重复店铺名的~
            boolean exists = false;

            if (!shops.isEmpty()) {
                // 检查店铺名是否已存在
                exists = shops.stream().anyMatch(p -> p.getShopName().equals(name));
            }


            if (!exists) {
                //创建店铺
                Shop s1 = new Shop(merchant, name);
                MainScreen.shops.add(s1);
                setThisShops();
                break;
            } else {
                System.out.println("店铺名已存在，请重新输入！");
            }
        }
    }

    /**
     * 删除店铺(这里其实少考虑很多东西，店铺实际上肯定是不能随便删除的，涉及多方利益纠纷等...要先处理好后最终才允许删除，这里简化不考虑！！！)
     */
    private void deleteShop() {
        System.out.println("======================删除店铺========================");
        if (shops.isEmpty()) {
            System.out.println("当前您还没有任何店铺噢~");
        } else {
            while (true) {
                System.out.println("请输入店铺名：");
                String name = sc.next();

                boolean exists = false;

                if (!shops.isEmpty()) {
                    // 检查店铺名是否已存在
                    exists = shops.stream().anyMatch(p -> p.getShopName().equals(name));
                }


                if (exists) {
                    while (true) {
                        //最后的确认！！！
                        System.out.println("您确认要删除店铺" + name + "吗？ 确认请输入yes，取消请输入no：");
                        String choose = sc.next();
                        switch (choose) {
                            case "yes":
                                break;
                            case "no":
                                return;
                            default:
                                System.out.println("请输入yes 或 no ！");
                        }

                        //选了yes
                        //根据店铺名获取店铺的索引
                        Shop s = MainScreen.shops.stream().filter(z -> z.getShopName().equals(name)).findFirst().get();
                        //删除店铺
                        MainScreen.shops.remove(s);
                        //更新这个界面的信息
                        setThisShops();
                        System.out.println("删除店铺成功！！！");
                        return;
                    }

                } else {
                    System.out.println("店铺名不存在，请重新输入！");
                }

            }
        }
    }

    /**
     * 编辑店铺
     */
    private void updateShop() {
        System.out.println("======================编辑店铺========================");
        if (shops.isEmpty()) {
            System.out.println("当前您还没有任何店铺噢~");
        } else {
            while (true) {
                System.out.println("请输入店铺名：");
                String name = sc.next();

                boolean exists = false;

                if (!shops.isEmpty()) {
                    // 检查店铺名是否已存在
                    exists = shops.stream().anyMatch(p -> p.getShopName().equals(name));
                }

                if (exists) {
                    // 根据店铺名获取店铺
                    Shop shop = shops.stream().filter(z -> z.getShopName().equals(name)).findFirst().get();

                    //前端是可以传空过来后端的，所以我这里这样设计，不过这里控制台不太能传空的样子？
                    System.out.println("请输入新的店铺名（留空则不修改）：");
                    String newShopName = sc.next();
                    if (!newShopName.isEmpty()) {
                        shop.setShopName(newShopName);
                    }

                    System.out.println("编辑店铺成功！！！");
                    break;
                } else {
                    System.out.println("店铺名不存在，请重新输入！");
                }
            }
        }

    }

    /**
     * 查看所有店铺
     */
    private void selectAllShops() {
        System.out.println("======================查看自己的店铺========================");
        if (shops.isEmpty()) {
            System.out.println("当前您还没有任何店铺噢~");
        } else {
            for (Shop shop : shops) {
                System.out.println(shop);
            }
        }
    }

    /**
     * 进入自己的店铺
     */
    private void goToShop() {
        System.out.println("======================进入店铺========================");
        if (shops.isEmpty()) {
            System.out.println("当前您还没有任何店铺噢~");
        } else {
            while (true) {
                System.out.println("请输入店铺名：");
                String name = sc.next();

                boolean exists = false;

                if (!shops.isEmpty()) {
                    // 检查店铺名是否已存在
                    exists = shops.stream().anyMatch(p -> p.getShopName().equals(name));
                }


                if (exists) {
                    //根据店铺名获取店铺
                    Shop s = shops.stream().filter(z -> z.getShopName().equals(name)).findFirst().get();
                    //进入店铺界面
                    BookScreen bookScreen = new BookScreen(s);
                    bookScreen.start();
                    return;

                } else {
                    System.out.println("店铺名不存在，请重新输入！");
                }

            }
        }

    }
}
